import * as accounService from '../services/AccountDivi';
export default {
    namespace: 'BaseEvaCalc',
    state: {
        params: { jg_dbbzlx: '1', jg_dbbzlxxs: '1' },
        results: {}
    },
    effects: {
        *setParam({ payload: { key, value, isFtp } }, { call, put }) {
            if (isFtp) {
                yield put({
                    type: 'getftp',
                    payload: {
                        key, pano: value
                    },
                });
            } else {
                yield put({
                    type: 'saveParams',
                    payload: {
                        keyVals: { [key]: value }
                    },
                });
            }
        },
        *getftp({ payload: { key, pano } }, { call, put, select }) {
            const m = yield select(state => state.BaseEvaCalc);
            const data = yield call(accounService.GetFTP, { key: pano });
            if (data) {
                let ftp = parseFloat(data.ftp)
                let day = Number(data.days)
                let ywpz = Number(m.params.zc_ywpz)
                if (key === "zc_ftp" && m.params.zc_ywpzxs) {
                    ftp = ftp + m.params.zc_ywpzxs
                    /**
                     * 个人经营性贷款FTP调整为1年以内（含）-0.35%、1年以上-0.4%。
                       个人消费贷款FTP调整为1年以内（含）-0.35%、1年以上-0.4%。
                       个人商用房贷款FTP调整为1年以内（含）-0.15%、1年以上-0.2%。
                    */
                } else if (key === "zc_ftp" && !m.params.zc_ywpzxs) {
                    if ((ywpz === 17 || ywpz === 18) && day <= 365) {
                        ftp = ftp - 0.35
                    } else if ((ywpz === 17 || ywpz === 18) && day > 365) {
                        ftp = ftp - 0.4
                    } else if (ywpz === 21 && day <= 365) {
                        ftp = ftp - 0.15
                    } else if (ywpz === 21 && day > 365) {
                        ftp = ftp - 0.2
                    }
                }
                yield put({
                    type: 'saveParams',
                    payload: {
                        keyVals: { [key]: pano, [key + 'xs']: ftp, [key + 'ts']: data.days }
                    },
                });
            }
        },
    },
    reducers: {
        saveParams(state, { payload: { keyVals } }) {
            let params = { ...state.params, ...keyVals }
            const { zs_je, zs_fl, zs_qx } = params
            const keyVals2 = {}
            if (zs_je && zs_fl && zs_qx) {
                const je = parseFloat(zs_je)
                const fl = parseFloat(zs_fl)
                const qx = parseFloat(zs_qx)
                keyVals2.zs_sqzs = (je * 10000 * fl / 100 * qx / 365).toFixed(2)
            }
            if (keyVals['jg_bzrpjxs']) {
                keyVals2.jg_dbbzlxxs = keyVals['jg_bzrpjxs']
            }
            params = { ...params, ...keyVals2 }
            return { ...state, params };
        },
        setResults(state, { payload: { keyVals } }) {
            let results = { ...state.results, ...keyVals }
            return { ...state, results };
        }
    }
};
